package com.superzlc.vcontroller;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import com.superzlc.vcontroller.ctrlwidget.SteeringWheelView;
import com.superzlc.vcontroller.ctrlwidget.XButtonView;

public class CtrlBoard1View extends ViewGroup {

	public CtrlBoard1View(Context context) {
		super(context);
		steeringWheel = new SteeringWheelView(this.getContext());
		clutch = new XButtonView(this.getContext());
		brakes = new XButtonView(this.getContext());
		throttle = new XButtonView(this.getContext());
		this.addView(steeringWheel);
		this.addView(clutch);
		this.addView(brakes);
		this.addView(throttle);
	}

	SteeringWheelView steeringWheel;
	XButtonView clutch;
	XButtonView brakes;
	XButtonView throttle;

	public int getAsixSteeringWheel() {
		return steeringWheel.getAsix();
	}
	public int getAsixClutch() {
		return clutch.getAsix();
	}
	public int getAsixBrakes() {
		return brakes.getAsix();
	}
	public int getAsixThrottle() {
		return throttle.getAsix();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 计算位置
		LayoutParams1 lp = (LayoutParams1)steeringWheel.getLayoutParams();
		lp.height = 640;
		lp.width = 640;
		lp.left = 0;
		lp.top = 0;

		LayoutParams1 lp1 = (LayoutParams1)clutch.getLayoutParams();
		lp1.height = 200;
		lp1.width = 600;
		lp1.left = 100;
		lp1.top = 10 + 640 + 550;

		LayoutParams1 lp2 = (LayoutParams1)brakes.getLayoutParams();
		lp2.height = 200;
		lp2.width = 600;
		lp2.left = 100;
		lp2.top = 10 + 640 + 200 + 550;

		LayoutParams1 lp3 = (LayoutParams1)throttle.getLayoutParams();
		lp3.height = 200;
		lp3.width = 600;
		lp3.left = 100;
		lp3.top = 10 + 640 + 200 + 400 + 350;

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// 设置位置
		LayoutParams1 lp = (LayoutParams1)steeringWheel.getLayoutParams();
		steeringWheel.layout(lp.left, lp.top, lp.left + lp.width, lp.top + lp.height);

		LayoutParams1 lp1 = (LayoutParams1)clutch.getLayoutParams();
		clutch.layout(lp1.left, lp1.top, lp1.left + lp1.width, lp1.top + lp1.height);

		LayoutParams1 lp2 = (LayoutParams1)brakes.getLayoutParams();
		brakes.layout(lp2.left, lp2.top, lp2.left + lp2.width, lp2.top + lp2.height);

		LayoutParams1 lp3 = (LayoutParams1)throttle.getLayoutParams();
		throttle.layout(lp3.left, lp3.top, lp3.left + lp3.width, lp3.top + lp3.height);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}

	static class LayoutParams1 extends LayoutParams {
		int left;
		int top;
		public LayoutParams1(Context c, AttributeSet attrs) {
			super(c, attrs);
		}

		public LayoutParams1(LayoutParams source) {
			super(source);
		}

		public LayoutParams1(int width, int height) {
			super(width, height);
		}
	}

	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams1(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(
			ViewGroup.LayoutParams p) {
		return new LayoutParams1(p);
	}

	@Override
	public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new LayoutParams1(this.getContext(), attrs);
	}
}
